<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>
        <h2>promise抽奖</h2>
        <button id="btn">抽奖</button>
    </div>
    <script>
        function rand(m,n){
            return Math.ceil(Math.random()*(n-m+1))+m-1;
        }
        var btn=document.querySelector('#btn');
        btn.addEventListener('click',function(){
        //创建实例
            const p=new Promise((resolve,reject)=>{
                setTimeout(() => {
                    let n=rand(1,100);
                    if(n<=30){
                        resolve(n);//将promise对象状态设置为【成功】,把n作为结果值传到成功事件
                    }else{
                        reject(n);//【失败】
                    }
                }, 100);
            });
        //调用then方法
            p.then((value)=>{
                alert('恭喜你中奖了，中奖号码是'+value);//成功事件
            },(reason)=>{
                alert('太遗憾了,号码是'+reason);//失败事件
            })
        })
    </script>
</body>
</html>